package com.zhang.hadoop.testMain;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.lettuce.core.output.StatusOutput;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.impl.xb.xsdschema.Facet;
import sun.java2d.pipe.SpanIterator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhang yufei
 * @date 2023/6/28 20:17
 **/
public class Test10 {

    public static void main(String args[]) throws Exception {
        //get1();
        //List<String> strings = get2();
        //List<String> strings1 = get3();
        //Set<String> list3=new HashSet<>();
        //int num1 = 0;
        //for(String s1:strings){
        //    for(String s2:strings1){
        //        Pattern pattern = Pattern.compile(s2);
        //        Matcher matcher = pattern.matcher(s1);
        //        Boolean flag=false;
        //        while (matcher.find()) {
        //            num1++;
        //            flag=true;
        //        }
        //        if(!flag){
        //            list3.add(s1);
        //        }
        //    }
        //}
        //for(String s1:list3){
        //    System.out.println(s1);
        //}
        //System.out.println(num1);
        //get4();
        //Map<String, Integer> stringIntegerMap = get5();
        //get6(stringIntegerMap);
        //Map<String, Integer> stringStringMap = get7();
        //Map<String, Integer> stringStringMap1 = get8();
        //List<String> list = new ArrayList<>();
        //Map<String, Integer> map = new HashMap<>();
        //for (Map.Entry<String, Integer> entry : stringStringMap1.entrySet()) {
        //    if (stringStringMap.containsKey(entry.getKey())) {
        //        Integer v1 = stringStringMap.get(entry.getKey());
        //        Integer v2 = entry.getValue();
        //        if (!Objects.equals(v1, v2)) {
        //            map.put(entry.getKey(), v2 - v1);
        //        }
        //    } else {
        //        list.add(entry.getKey()+"\t"+entry.getValue());
        //    }
        //}
        //for (Map.Entry<String, Integer> entry : map.entrySet()) {
        //    System.out.println(entry.getKey() + "\t" + entry.getValue());
        //}
        //System.out.println("---------------------------");
        //for(String key:list){
        //    System.out.println(key);
        //}
        List<String> list1 = get9();
        List<String> list2 = get10();
        for (String s1 : list2) {
            if (!list1.contains(s1)) {
                System.out.println(s1);
            }
        }
    }

    public static List<String> get1() throws Exception {
        String file = "/Users/zhangyufei/0-program/work/idea/hadoop/base/src/main/java/com/zhang/hadoop/txt/json1.json";
        FileInputStream inputStream = new FileInputStream(file);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String str = null;
        Map<String, String> map = new HashMap<>();
        String key = null;
        while ((str = bufferedReader.readLine()) != null) {
            String[] split = str.split("：");
            if (split.length > 1) {
                //if (split[0].contains("2d") || split[0].contains("3d") || split[0].contains("4d") || split[0].contains("5d") || split[0].contains("6d")
                //        || split[0].contains("7d") || split[0].contains("15d") || split[0].contains("30d") || split[0].contains("90d")
                //        || split[0].contains(".D") ||
                //        split[0].contains("1D") || split[0].contains("2D") || split[0].contains("3D") || split[0].contains("4D") || split[0].contains("5D") || split[0].contains("6D")
                //        || split[0].contains("7D") || split[0].contains("15D") || split[0].contains("30D") || split[0].contains("90D")) {
                //    continue;
                //}
                if (split[0].contains("staffFeature") || split[0].contains("jobFeature")) {
                    continue;
                }
                map.put(split[0].replaceAll("\"", "").trim(), split[1].replaceAll("\"", "").replaceAll(",", ""));
            }
        }
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey());
        }
        //close
        inputStream.close();
        bufferedReader.close();
        return null;
    }

    public static List<String> get2() throws Exception {
        List<String> list = new ArrayList<>();
        String file = "/Users/zhangyufei/0-program/work/idea/hadoop/base/src/main/java/com/zhang/hadoop/txt/text06";
        FileInputStream inputStream = new FileInputStream(file);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String str = null;
        Map<String, String> map = new HashMap<>();
        String key = null;
        while ((str = bufferedReader.readLine()) != null) {

            list.add(str.trim());
        }
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey());
        }
        //close
        inputStream.close();
        bufferedReader.close();
        return list;
    }

    public static List<String> get3() throws Exception {
        List<String> list = new ArrayList<>();
        String file = "/Users/zhangyufei/0-program/work/idea/hadoop/base/src/main/java/com/zhang/hadoop/txt/text07";
        FileInputStream inputStream = new FileInputStream(file);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String str = null;
        Map<String, String> map = new HashMap<>();
        String key = null;
        while ((str = bufferedReader.readLine()) != null) {
            if (StringUtils.isBlank(str)) {
                continue;
            }
            if (str.contains("2d") || str.contains("3d") || str.contains("4d") || str.contains("5d") || str.contains("6d")
                    || str.contains("7d") || str.contains("15d") || str.contains("30d") || str.contains("90d")
                    || str.contains(".D") ||
                    str.contains("1D") || str.contains("2D") || str.contains("3D") || str.contains("4D") || str.contains("5D") || str.contains("6D")
                    || str.contains("7D") || str.contains("15D") || str.contains("30D")) {
                continue;
            }
            list.add(str.trim());
        }
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey());
        }
        //close
        inputStream.close();
        bufferedReader.close();
        return list;
    }

    public static void get4() throws Exception {
        String file = "/Users/zhangyufei/1-我的/7-文档/工作簿1.xlsx";
        FileInputStream in = new FileInputStream(file);
        Map<String, List<String>> map = new HashMap<>();
        Workbook wk = new XSSFWorkbook(in);
        //Pattern pattern = Pattern.compile("(?<=from\\s)\\S+");
        //Pattern pattern = Pattern.compile("^*dp:.*$");
        Pattern pattern = Pattern.compile("^*topic.*$");
        //Pattern pattern = Pattern.compile("^(?:dwd.|dwdd.|dwb.|dmr.|dws.|sdf.|dp:|bd:|crm.|myhive)\\S*(?<![\\s\\n])$");
        for (int a = 0; a <= 3; a++) {
            Sheet sheet = wk.getSheetAt(a);
            int count = sheet.getLastRowNum();
            String key = "";
            for (int i = 0; i < count; i++) {
                Row row = sheet.getRow(i);
                String key1 = row.getCell(3).getStringCellValue();
                if (StringUtils.isBlank(key1)) {
                    key1 = key;
                } else {
                    key = key1;
                }
                Cell cell = row.getCell(4);
                if (cell.getCellType().equals(CellType.STRING)) {
                    String s1 = cell.getStringCellValue();
                    Matcher matcher = pattern.matcher(s1);
                    while (matcher.find()) {
                        if (map.containsKey(key1)) {
                            map.get(key1).add(matcher.group().replace("myhive.", ""));
                        } else {
                            List<String> list = new ArrayList<>();
                            list.add(matcher.group().replace("myhive.", ""));
                            map.put(key1, list);
                        }
                    }
                }
            }
        }
        int num1 = 0;
        int num2 = 0;
        int num3 = 0;
        Set<String> set = new HashSet<>();
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            System.out.println(entry.getKey() + "\t" + JSONObject.toJSONString(entry.getValue()));
            if (entry.getValue().size() > 1) {
                num2++;
            } else {
                num1++;
            }
            for (String table : entry.getValue()) {
                set.add(table);
                num3++;
            }
        }
        System.out.println("单个表:" + num1 + ",多个表:" + num2 + ",总共from:" + num3);
        set.stream().forEach(data -> {
            System.out.println(data);
        });
    }

    public static Map<String, Integer> get5() throws Exception {
        Map<String, Integer> map = new HashMap<>();
        String file = "/Users/zhangyufei/0-program/work/idea/hadoop/base/src/main/java/com/zhang/hadoop/txt/text09";
        FileInputStream inputStream = new FileInputStream(file);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String str = null;
        String key = null;
        while ((str = bufferedReader.readLine()) != null) {
            String[] split = str.split("\t");
            map.put(split[0], JSONArray.parseArray(split[1]).size());
        }
        //for (Map.Entry<String, String> entry : map.entrySet()) {
        //    System.out.println(entry.getKey());
        //}
        //close
        inputStream.close();
        bufferedReader.close();
        return map;
    }

    public static void get6(Map<String, Integer> stringIntegerMap) throws Exception {
        Map<String, Integer> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        String file = "/Users/zhangyufei/0-program/work/idea/hadoop/base/src/main/java/com/zhang/hadoop/txt/text06";
        FileInputStream inputStream = new FileInputStream(file);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            list.add(str.trim());
        }
        int num1 = 0;
        int num2 = 0;
        for (String key : list) {
            for (Map.Entry<String, Integer> entry : stringIntegerMap.entrySet()) {
                Pattern pattern = Pattern.compile(entry.getKey());
                Matcher matcher = pattern.matcher(key);
                while (matcher.find()) {
                    if (entry.getValue() > 1) {
                        num2++;
                    } else {
                        num1++;
                    }
                }
            }
        }
        System.out.println("单个表:" + num1 + ",多个表:" + num2);
        inputStream.close();
        bufferedReader.close();
    }

    public static Map<String, Integer> get7() throws Exception {
        Map<String, Integer> map = new HashMap<>();
        String file = "/Users/zhangyufei/0-program/work/idea/hadoop/base/src/main/java/com/zhang/hadoop/txt/text09";
        FileInputStream inputStream = new FileInputStream(file);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            String[] split = str.split("\t");
            Integer num = Integer.parseInt(split[1]);
            if (map.containsKey(split[0])) {
                map.put(split[0], map.get(split[0]) + num);
            } else {
                map.put(split[0], num);
            }
        }
        inputStream.close();
        bufferedReader.close();
        return map;
    }

    public static Map<String, Integer> get8() throws Exception {
        Map<String, Integer> map = new HashMap<>();
        String file = "/Users/zhangyufei/0-program/work/idea/hadoop/base/src/main/java/com/zhang/hadoop/txt/text10";
        FileInputStream inputStream = new FileInputStream(file);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            String[] split = str.split("\t");
            Integer num = Integer.parseInt(split[1]);
            if (map.containsKey(split[0])) {
                map.put(split[0], map.get(split[0]) + num);
            } else {
                map.put(split[0], num);
            }
        }
        inputStream.close();
        bufferedReader.close();
        return map;
    }

    public static List<String> get9() throws Exception {
        List<String> list = new LinkedList<>();
        String file = "/Users/zhangyufei/0-program/work/idea/hadoop/base/src/main/java/com/zhang/hadoop/txt/text07";
        FileInputStream inputStream = new FileInputStream(file);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            list.add(str);
        }
        inputStream.close();
        bufferedReader.close();
        return list;
    }

    public static List<String> get10() throws Exception {
        List<String> list = new LinkedList<>();
        String file = "/Users/zhangyufei/0-program/work/idea/hadoop/base/src/main/java/com/zhang/hadoop/txt/text06";
        FileInputStream inputStream = new FileInputStream(file);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            list.add(str);
        }
        inputStream.close();
        bufferedReader.close();
        return list;
    }
}
